package com.zxcz.aliyun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.DeleteDeviceRequest;
import com.aliyuncs.iot.model.v20180120.DeleteDeviceResponse;
import com.aliyuncs.iot.model.v20180120.QueryDeviceRequest;
import com.aliyuncs.iot.model.v20180120.QueryDeviceResponse;
import com.aliyuncs.iot.model.v20180120.QueryProductListRequest;
import com.aliyuncs.iot.model.v20180120.QueryProductListResponse;
import com.aliyuncs.iot.model.v20180120.QueryProductRequest;
import com.aliyuncs.iot.model.v20180120.QueryProductResponse;
import com.aliyuncs.iot.model.v20180120.RegisterDeviceRequest;
import com.aliyuncs.iot.model.v20180120.RegisterDeviceResponse;
import com.aliyuncs.profile.DefaultProfile;

@Component
public class IotManager {
	@Autowired
	private IotConfig iotConfig;
	
	/**
	 * 获取IoT客户端对象
	 */
	private DefaultAcsClient getAcsClient() {
		try {
			DefaultProfile.addEndpoint(iotConfig.getRegionId(), iotConfig.getRegionId(), "Iot", "iot.cn-shanghai.aliyuncs.com");
			DefaultProfile profile = DefaultProfile.getProfile(iotConfig.getRegionId(), iotConfig.getAccessKey(), iotConfig.getAccessSecret());
			return new DefaultAcsClient(profile);
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询产品列表信息
	 */
	public QueryProductListResponse queryProductList(Integer currentPage,Integer pageSize) {
		DefaultAcsClient client = getAcsClient();
		if(client == null) {
			System.out.println("获取iot客户端对象失败");
			return null;
		}
		 QueryProductListRequest request = new QueryProductListRequest();
        // 分页页码（必选参数）
        request.setCurrentPage(currentPage);
        // 每页显示条数 最大值是200。（必选参数）
        request.setPageSize(pageSize);
        
        try {
        	// 发起请求并获取返回值
			QueryProductListResponse response = client.getAcsResponse(request);
			return response;
		} catch (ClientException e) {
			e.printStackTrace();
		}
        
        return null;
	}
	
	/**
	 * 查询产品详细信息
	 */
	public QueryProductResponse QueryProduct(String productKey) {
		DefaultAcsClient client = getAcsClient();
		if(client == null) {
			System.out.println("获取iot客户端对象失败");
			return null;
		}
		 // 创建请求接口
        QueryProductRequest request = new QueryProductRequest();
        // 要查询的产品的ProductKey。
        request.setProductKey(productKey);
        
        try {
        	// 发起请求并获取返回值
			QueryProductResponse response = client.getAcsResponse(request);
			return response;
		} catch (ClientException e) {
			e.printStackTrace();
		}
        
        return null;
	}
	
	/**
	 * 注册设备
	 */
	public RegisterDeviceResponse registerDevice(String productKey, String deviceName) {
		DefaultAcsClient client = getAcsClient();
		if(client == null) {
			System.out.println("获取iot客户端对象失败");
			return null;
		}
		// 创建请求接口
		RegisterDeviceRequest request = new RegisterDeviceRequest();
		// 待注册设备所隶属的产品的Key，即产品的唯一标识符。
		request.setProductKey(productKey);
		// 为待注册的设备命名。如果不传入该参数，则由系统随机生成设备名称。
		request.setDeviceName(deviceName);
		
		try {
			// 发起请求并获取返回值
			RegisterDeviceResponse response = client.getAcsResponse(request);
			return response;
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 删除设备
	 */
	public DeleteDeviceResponse deleteDevice(String productKey, String deviceName) {
		DefaultAcsClient client = getAcsClient();
		if(client == null) {
			System.out.println("获取iot客户端对象失败");
			return null;
		}
		DeleteDeviceResponse response = null;
		 // 创建请求接口
		DeleteDeviceRequest request = new DeleteDeviceRequest();
        request.setProductKey(productKey);
        request.setDeviceName(deviceName);
        
        try {
        	// 发起请求并获取返回值
			response = client.getAcsResponse(request);
			return response;
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询设备列表
	 */
	public QueryDeviceResponse queryDevice(String productKey,
            Integer currentPage,Integer pageSize) {
		DefaultAcsClient client = getAcsClient();
		if(client == null) {
			System.out.println("获取iot客户端对象失败");
			return null;
		}
		// 创建请求接口
		QueryDeviceRequest request = new QueryDeviceRequest();
		// 分页显示第几页
		request.setCurrentPage(currentPage);
		// 分页指定每页显示条数
		request.setPageSize(pageSize);
		// 要查询的设备所隶属的产品Key。
		request.setProductKey(productKey);
		
		try {
			// 发起请求并获取返回值
			QueryDeviceResponse response = client.getAcsResponse(request);
			return response;
		} catch (ClientException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
